package Review;

import java.util.List;
import java.util.Stack;

//单链表的实现
/*Ctrl + R 选中类中相同的变量名称然后，改名*/
public class MySingleList {
    public int val;
    //内部类如果是static的，生成对象的时候是不依赖于外部类对象的
    //不加static，生成对象的时候依赖于外部类对象
    static class ListNode {
        public int val;//存储值
        public ListNode next;//存储next域
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;//head引用的是当前列表的头节点

    public void createLink() {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        head = listNode1;
        //当这个方法走完，node1，2，3，4....都被回收了，
        //他们都是局部变量
    }

    //递归打印链表(逆序打印链表)
    public void display3(ListNode pHead) {
        if (pHead == null) return;
        if (pHead.next == null) {
            System.out.print(pHead.val + " ");
            return;
        }
        display3(pHead.next);
        System.out.print(pHead.val + " ");
    }

    //用栈结构打印链表(逆序打印链表)
    public void display4() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        //遍历栈
        while (!stack.isEmpty()) {
            ListNode top = stack.pop();
            System.out.print(top.val + " ");
        }
        System.out.println();
    }
    //遍历打印链表
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //从指定位置开始打印链表
    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //查找是否包含关键字key，是否在单链表中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) return true;
            cur = cur.next;
        }
        return false;
    }
    //得到单链表的长度
    public int size() {
        ListNode cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    //头插法
    public void addFirst(int data){
        ListNode listNode = new ListNode(data);
        listNode.next = head;
        head = listNode;
    }
    //尾插法(考虑如果当前链表没有节点)
    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if (head == null) {
            head = listNode;
            return;//不要忘了return
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = listNode;
    }
    //指定下标位置插入节点(记得考虑下标为0的时候就是头插法)
    /*public void addIndex(int index, int data) {
        Node node = new Node(data);
        //判断是否合法的下标
        checkIndex(index);
        Node cur = head;
        if (index == 0) {
            addFirst(data);
        }
        Node res = new Node(-1);
        res.next = head;
        while (index > 0) {
            cur = cur.next;
            res = res.next;
            index--;
        }
        node.next = cur;
        res.next = node;
    }*/

    //指定下标位置插入节点
    public void addIndex(int index, int data) {
        checkIndex(index);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode listNode = new ListNode(data);
        ListNode cur = findIndexSubOne(index);//cur走到下标的前一个位置
        listNode.next = cur.next;
        cur.next = listNode;
    }
    //找到index-1位置的节点的地址
    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    private void checkIndex(int index) {
        if (index < 0 || index > size()) {
            throw new ListIndexOutOfException("下标位置不合法！");
        }
    }

    //删除第一次出现的关键字为key的节点
    /*public void remove(int key) {
        Node cur = head;
        Node res = new Node(-1);
        res.next = head;
        if (head.val == key) {
            head = head.next;
        }
        while (cur.val != key) {
            cur = cur.next;
            res = res.next;
        }
        res.next = cur.next;
    }*/
    //删除第一次出现的关键字为key的节点
    public void remove(int key) {
        if (head == null) return;
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        cur.next = cur.next.next;
    }
    //找到关键字key的前一个节点
    private ListNode searchPrev(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除所有值为key的节点（遍历一遍链表就可以删除所有值为key的节点）
    public void removeAllKey(int key) {
        if (head == null) return;
        //这里的if需要换成while，因为可能head节点就有连续的需要删除的节点
        while (head.val == key) {
            head = head.next;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //也可以在这里写为if，此时遍历一遍链表之后就只剩下头节点没有删除
        /*if (head.val == key) {
            head = head.next;
        }*/
    }

    //清空链表: 保证链表中所有的节点都可以被回收
    public void clear() {
        head = null;
    }

    //链表的反转
    public ListNode ReverseList() {
        if (head == null) return null;
        if (head.next == null) return head;
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            //头插法
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    //返回链表的中间节点
    public ListNode middleNode(ListNode head) {
        if (head == null) return null;
        ListNode s = head;
        ListNode f = head;
        //此时要注意，f走一步就需要判断是否为空，否则可能走一步就
        //空指针异常了
        while (f != null && f.next != null) {
            f = f.next.next;
            s = s.next;
        }
        //结束的情况: fast = null && fast.next = null
        return s;
    }
    //返回链表的倒数第k个节点
    public ListNode FindKthToTail (ListNode pHead, int k) {
        // write code here
        //快指针先走k-1步，然后和慢指针一起走,此时返回的慢指针就是倒数第k个节点
        if (k < 0 || pHead == null) return null;
        ListNode cur = pHead;
        int len = 0;
        while (cur != null) {
            cur = cur.next;
            len++;
        }
        ListNode fast = pHead;
        ListNode slow = pHead;
        while (k !=  1) {
            fast = fast.next;
            if (fast == null) return null;
            k--;
        }
        //fast和slow一起走
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //合并两个有序的链表
    public ListNode Merge(ListNode list1, ListNode list2) {
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                tmp.next = list1;
                list1 = list1.next;
                tmp = tmp.next;
            }else {
                tmp.next = list2;
                list2 = list2.next;
                tmp = tmp.next;
            }
        }
        //如果list1为空了
        if (list2 != null) {
            tmp.next = list2;
        }
        if (list1 != null) {
            tmp.next = list1;
        }
        return newHead.next;
    }
    //判断链表是否是回文结构
    public boolean isPail (ListNode head) {
        if (head ==null) return false;
        if (head.next == null) return true;
        //1.找中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.反转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3.一个从头往后走，一个从后往前走
        //这里不要和fast指针的值进行比较，fast偶数情况下走到最后已经空指针了
        //也不能和cur进行比较，因为最后cur在什么情况下都会空指针
        while (slow != head) {
            if (head.val != slow.val) {
                return false;
            }
            //偶数的情况
            if (head.next == slow) {
                return true;
            }
            slow = slow.next;
            head = head.next;
        }
        return true;
    }
    //链表分割
    /*思路：分成两个段，第一个段放小于x的节点，第二个段放大于x的节点，然后把两个段
    * 连起来，此时要考虑是否同时有小于x的和大于x的数据，之后要考虑串起来之后的最后
    * 一个节点next是否需要置空*/
    public ListNode partition(int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = head;
        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //有可能不会同时存在小于x和大于等于x的数据
        //如果第一个段中没有数据，就返回第二个段，如果第二个段还没有数据
        //此时直接返回null，
        if (bs == null) return as;
        //第一个段不为空
        be.next = as;
        //如果第二个段不为空，也就是有>x的数据，此时需要把最后一个节点next域置空
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }
    //相交链表
    public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
        //1.先求长度
        ListNode cur1 = headA, cur2 = headB;
        int len1 = 0, len2 = 0;
        while (cur1 != null) {
            len1++;
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            len2++;
            cur2 = cur2.next;
        }
        //2.让比较长的链表的cur 走差值的步数
        cur1 = headA;//这个地方一定要注意：让cur1和cur2指回来
        cur2 = headB;//因为求完长度之后，cur1 和 cur2 都已经是空的了
        int len = Math.abs(len1 - len2);
        while (len != 0) {
            if (len1 < len2) {
                cur2 = cur2.next;
            }else {
                cur1 = cur1.next;
            }
        }
        //2.然后再让cur1 和 cur2 一起走
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA,ps = headB;
        int len1 = 0,len2 = 0;
        while (pl != null) {
            len1++;
            pl = pl.next;
        }
        while (ps != null) {
            len2++;
            ps = ps.next;
        }
        //2.让pl和ps 指回来
        pl = headA;
        ps = headB;
        int len = len1 - len2;
        //3.根据len的值修改pl 和 ps 的指向
        if (len < 0) {
            pl = headB;
            ps = headA;
            len = len2 - len1;
        }//此时len一定是一个正数 pl一定指向的是最长的  ps一定指向的是最短的
        while (len != 0) {
            pl = pl.next;
            len--;
        }
        while (pl != ps) {//这里可以不用判断pl和ps是否是空的，如果是空的
            //此时返回的是ps或者pl，返回的也是null
            pl = pl.next;
            ps = ps.next;
        }
        return ps;
    }
    //判断链表是否有环
    //快慢指针为啥不能走三步，走一步：如果有链表有环且只有两个节点，此时会错过，
    //永远也不会相遇
    //但是如果是走两步和走一步的情况，此时fast和slow最多差一个环的长度，此时
    //fast和slow是一次追击一步，如果存在环一定会相遇
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }
    public boolean hasCycle2(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        //或者走到这里肯定有一个是空的了
        if (fast == null || fast.next == null) {
            return false;
        }
        return true;
    }
    //创建一个环
    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next.next;
    }
    //环形链表：返回链表开始入环的第一个节点
    //结论：让一个指针从链表的起始位置开始遍历链表，同时让一个指针从相遇点开始绕环遍历
    //两个指针每次都是走一步，最终一定会在入口点相遇
    public ListNode detectCycle(ListNode head) {
        //先判断是否有环
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        //或者走到这里肯定有一个是空的了
        if (fast == null || fast.next == null) {
            return null;
        }
        //走到这里一定是有环
        slow = head;
        while (slow != fast) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
